// pages/mine/shaer/shaer.js
let { AJAX, timeTranfer,imgUrl} = require("../../../utils/util.js")
const App = getApp()
Page({

   /**
    * 页面的初始数据
    */
   data: {
      navH: 0,
      is_code: false,
      firstCodeUrl:'',
      codeUrl: '', 
      info:'',
     imgUrl: imgUrl
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {

      let vm = this      
      this.setData({
         navH: App.globalData.navHeight,
         is_code: wx.getStorageSync('is_code'),
         firstCodeUrl:wx.getStorageSync('firstCodeUrl')
      })
      if (!wx.getStorageSync("ACCESSTOKEN")) {
         App.userInfoReadyCallback = res => {
            wx.setStorageSync('ACCESSTOKEN', res);
            vm.getMemberId()   
         }
         return
      }

      vm.getMemberId()     
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   }, 

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {

   },






   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function(res) {
      if(res.from=="button"){
         return {
            title: this.data.info.username + '邀请你成为行知达人',
            path: '/pages/mine/grow/growrecord/growrecord?member_id=' + this.data.info.member_id,
            imageUrl: '/image/banner1.png'
         }
      } else if (res.from == "menu"){
         return {
            title: this.data.info.username + '邀请你成为行知达人',
            path: '/pages/mine/grow/growrecord/growrecord?member_id=' + this.data.info.member_id,
            imageUrl: '/image/banner1.png'
         }
      }
     



   },
   // 获取用户信息

   // 获取member
   getMemberId(){
      AJAX('member/member-info').then(res => {
        
         this.setData({
            info: res.data,
         })
         wx.showToast({
            title: '加载中...',
            duration:500,
            mask: true,
            icon: 'loading'
         })
         if (this.data.is_code == false) {
            AJAX('qrcode/qrcode', 'POST', {
               url: 'pages/mine/grow/growrecord/growrecord',
               scene: res.data.member_id,
               type: "partner"
            }).then(res => {
               this.setData({
                  codeUrl: res.data.img
               })
            })
         }
         
      })

   },

   // 获取二维码

   saveShareImg: function() {
      var that = this;
      wx.showLoading({
         title: '正在保存',
         mask: true,
      })
      let imgSRc =this.data.firstCodeUrl || this.data.codeUrl
      setTimeout(function() {
         wx.downloadFile({
            url: imgSRc,
            success: function(res) {
               console.log(res);
               //图片保存到本地
               wx.saveImageToPhotosAlbum({
                  filePath: res.tempFilePath,
                  success: function(data) {
                     wx.showToast({
                        title: '保存成功',
                        icon: 'success',
                        duration: 2000
                     })
                     // 下载成功后删除二维码qrcode/del-image
                     AJAX('qrcode/del-image', 'POST', {
                        file_name: that.data.file_name
                     }).then(res => {

                        console.log("删除成功")
                     })
                  },
                  fail: function(res) {
                     if (res.errMsg == "saveImageToPhotosAlbum:fail cancel") {
                        // wx.showModal({
                        //    title: '保存图片失败',
                        //    content: '您已取消保存图片到相册！',
                        //    showCancel: false
                        // });
                        wx.showToast({
                           title: '保存图片失败,您已取消保存图片到相册！',
                           icon: 'none',
                           duration: 2000
                        })
                     } else {
                        wx.showModal({
                           title: '提示',
                           content: '保存图片失败，您可以点击确定设置获取相册权限后再尝试保存！',
                           complete: function(res) {
                              console.log(res);
                              if (res.confirm) {
                                 wx.openSetting({}) //打开小程序设置页面，可以设置权限
                              } else {
                                 wx.showToast({
                                    title: '保存图片失败,您已取消保存图片到相册！',
                                    icon: 'none',
                                    duration: 2000
                                 })
                              }
                           }
                        });
                     }
                  },
                  complete(res) {
                     console.log(res);
                  }
               })
            }
         })
      }, 1000);
   },
   navBack() {
      wx.navigateBack()
   }
})